package com.gitee.wsl.compose.ui.canvaspainter

import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.unit.toSize
import com.gitee.wsl.compose.ui.canvaspainter.utils.AppUtils.createPath

@Composable
internal fun CanvasPainterBoard(
    painterController: PainterController,
    modifier: Modifier = Modifier
) {
    val paintPath by painterController.paintPath.collectAsState()
    Spacer(modifier
        .fillMaxSize()
        .clipToBounds()
        .pointerInput(Unit) {
            detectDragGestures(
                onDragEnd = painterController::onDragEnd,
                //onDragStart = painterController::addPaintPath,
                onDragStart = painterController::inputEvent,
            ) { change, _ ->
                //painterController.updateLastPath(change.position)
                painterController.dragEvent(change.position)
            }
        }
        .drawBehind {
            painterController.canvasSize = this.size
            for (p in paintPath) {
                drawPath(
                    createPath(p.points),
                    color = p.color,
                    style = Stroke(
                        width = p.stroke,
                        cap = StrokeCap.Round
                    ),
                )
            }
        }
        .onSizeChanged {
            painterController.canvasSize = it.toSize()
        })
}